package com.chao.common.codeGeneration.bean;


import java.util.HashMap;
import java.util.Map;

/**
 * mysql表字段Bean
 * chao
 * 2020-07-07
 */
public class TableColumns {

    /**
     * 字段名
     */
    private String columnName;

    /**
     * 属性名称(首字母小写)
     */
    public String lowercaseName;

    /**
     * 属性名称(首字母大写)
     */
    public String capitalLetterName;

    /**
     * 数据类型 数据库
     */
    private String dataType;

    /**
     * 属性类型 java
     */
    public String beanType;

    /**
     * 注释
     */
    private String columnComment;

    /**
     * 是否主键
     */
    public Boolean key;

    /**
     * 是否列表中的最后一个字段 后边不加,
     */
    public Boolean end;

    /**
     * 是否必填
     */
    public Boolean required;
    public TableColumns(){}


    public TableColumns(String columnName,String dataType,String columnComment,Boolean required){
        this.columnName = columnName.toLowerCase();
        this.lowercaseName = getUp(this.columnName);
        this.capitalLetterName = this.lowercaseName.substring(0, 1).toUpperCase()+this.lowercaseName.substring(1);
        this.beanType = getJavaType(dataType);
        this.dataType = getMyType(this.beanType);
        this.columnComment = columnComment.replace(" ","").replace("\t","").replace("\n"," ");
        this.required = required;
    }

    /**
     * 将数据库字段转为驼峰命名
     */
    private String getUp(String key){
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i<key.length();i++){
            if(key.charAt(i) == '_'){
                i++;
                sb.append((char) (key.charAt(i)-32));
            }else{
                sb.append(key.charAt(i));
            }
        }
        return sb.toString();
    }

    /**
     * 将java数据类型转为MyBatis数据类型
     */
    private String getMyType(String type){
        Map<String,String> map = new HashMap();
        map.put("String","VARCHAR");
        map.put("BigDecimal","NUMERIC");
        map.put("Boolean","BOOLEAN");
        map.put("Integer","INTEGER");
        map.put("Long","BIGINT");
        map.put("Float","REAL");
        map.put("Double","DOUBLE");
        map.put("Date","TIMESTAMP");
        map.put("Clob","CLOB");
        map.put("Blob","BLOB");
        map.put("Array","ARRAY");
        map.put("Struct","STRUCT");
        map.put("Ref","REF");
        return map.get(type);
    }

    /**
     * 将mysql数据类型转为java数据类型
     */
    private String getJavaType(String type){
        Map<String,String> map = new HashMap();
        map.put("VARCHAR","String");
        map.put("LONGTEXT","String");
        map.put("CHAR","String");
        map.put("BLOB","byte[]");
        map.put("INTEGER","Long");
        map.put("INT","Integer");
        map.put("TINYINT","Integer");
        map.put("SMALLINT","Integer");
        map.put("MEDIUMINT","Integer");
        map.put("BIT","Boolean");
        map.put("BIGINT","BigInteger");
        map.put("FLOAT","Float");
        map.put("DOUBLE","Double");
        map.put("DECIMAL","BigDecimal");
        map.put("DATE","Date");
        map.put("TIME","Date");
        map.put("DATETIME","Date");
        map.put("TIMESTAMP","Date");
        map.put("YEAR","Date");
        return map.get(type.toUpperCase());
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getLowercaseName() {
        return lowercaseName;
    }

    public void setLowercaseName(String lowercaseName) {
        this.lowercaseName = lowercaseName;
    }

    public String getCapitalLetterName() {
        return capitalLetterName;
    }

    public void setCapitalLetterName(String capitalLetterName) {
        this.capitalLetterName = capitalLetterName;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getBeanType() {
        return beanType;
    }

    public void setBeanType(String beanType) {
        this.beanType = beanType;
    }

    public String getColumnComment() {
        return columnComment;
    }

    public void setColumnComment(String columnComment) {
        this.columnComment = columnComment;
    }

    public Boolean getKey() {
        return key;
    }

    public void setKey(Boolean key) {
        this.key = key;
    }

    public Boolean getEnd() {
        return end;
    }

    public void setEnd(Boolean end) {
        this.end = end;
    }

    public Boolean getRequired() {
        return required;
    }

    public void setRequired(Boolean required) {
        this.required = required;
    }
}